package com.example.ten.utils;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;

/*
 * 申请权限的工具类
 * */
public class PermissionUtils {
    public static final int CODE_READ_CALENDAR = 0;
    public static final int CODE_CAMERA = 1;
    public static final int CODE_CONTACTS = 2;
    public static final int CODE_LOCATION = 3;
    public static final int CODE_RECORD_AUDIO = 4;
    public static final int CODE_PHONE = 5;
    public static final int CODE_SENSORS = 6;
    public static final int CODE_SMS = 7;
    public static final int CODE_STORAGE = 8;
    //   权限名称
    public static final String PERMISSION_READ_CALENDAR = Manifest.permission.READ_CALENDAR;
    public static final String PERMISSION_CAMERA = Manifest.permission.CAMERA;
    public static final String PERMISSION_CONTACTS = Manifest.permission.READ_CONTACTS;
    public static final String PERMISSION_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
    public static final String PERMISSION_RECORD_AUDIO = Manifest.permission.RECORD_AUDIO;
    public static final String PERMISSION_PHONE = Manifest.permission.CALL_PHONE;
    public static final String PERMISSION_SENSORS = Manifest.permission.BODY_SENSORS;
    public static final String PERMISSION_SMS = Manifest.permission.READ_SMS;
    public static final String PERMISSION_STORAGE = Manifest.permission.WRITE_EXTERNAL_STORAGE;
    //    为了让请求码和下面权限名称，进行一一对应，使用数组存储权限，那么数组的角标正是请求码
    public static final String[]requestPermissions = {PERMISSION_READ_CALENDAR,PERMISSION_CAMERA,
            PERMISSION_CONTACTS,PERMISSION_LOCATION,PERMISSION_RECORD_AUDIO,PERMISSION_PHONE,
            PERMISSION_SENSORS,PERMISSION_SMS,PERMISSION_STORAGE
    };
    //    定义表示授权成功的接口
    public interface PermissionGrant{
        public void onPermissionGranted(int requestCode);
    }
    /*
     * 封装请求权限的函数
     * */
    public static void requestPermission(Activity activity,int requestCode,PermissionGrant permissionGrant){
        if (activity == null) {
            return;
        }
//        排除不存在的请求码
        if(requestCode<0||requestCode>=requestPermissions.length){
            return;
        }
        /* 获取请求的权限 */
        String requestPermission = requestPermissions[requestCode];
//        判断手机版本是否为6.0以上版本，如果小于6.0不用申请权限了
        if(Build.VERSION.SDK_INT<23){
            return;
        }
//        判断是否被授权了
        int checkSelfPermission = ActivityCompat.checkSelfPermission(activity, requestPermission);
        if (checkSelfPermission == PackageManager.PERMISSION_GRANTED) {  //已经被授权了
            permissionGrant.onPermissionGranted(requestCode);
            Log.i("animee", "requestPermission: 用户已经授权了！");
        }else {
//            用户没有授权，需要申请授权
            if (ActivityCompat.shouldShowRequestPermissionRationale(activity,requestPermission)) {
                shouldShowRationale(activity,requestCode,requestPermission);
            }else {
                ActivityCompat.requestPermissions(activity,new String[]{requestPermission},requestCode);
            }
        }
    }
    /*申请权限结果的方法*/
    public static void requestPermissionsResult(Activity activity,int requestCode,
                                                @NonNull String[] permissions, @NonNull int[] grantResults,
                                                PermissionGrant permissionGrant){
        if (activity == null) {
            return;
        }
//        过滤无效请求码
        if(requestCode<0||requestCode>=requestPermissions.length){
            Toast.makeText(activity,"无效请求码："+requestCode,Toast.LENGTH_SHORT).show();
            return;
        }
        if (grantResults.length ==1&&grantResults[0]== PackageManager.PERMISSION_GRANTED) {
//            授权成功了
            permissionGrant.onPermissionGranted(requestCode);
        }else {
            String permissionError = requestPermissions[requestCode];
//            需要让用户打开设置自动去设置权限
            openSettingActivity(activity,"those permession need granted");
        }
    }
    /* 当权限申请失败时，打开设置界面，让用户在设置界面当中打开权限，否则就无法进入activity*/
    private static void openSettingActivity(final Activity activity,String msg) {
        showMessageOKCancel(activity, msg, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
//                打开设置界面
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package",activity.getPackageName(),null);
                intent.setData(uri);
                activity.startActivity(intent);
            }
        });
    }

    /*
     * 提示用户需要权限的方法
     * */
    public static void shouldShowRationale(final Activity activity,final int requestCode,final String requestPermission){
        showMessageOKCancel(activity, "是否弹出" + requestPermission + "权限的申请", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ActivityCompat.requestPermissions(activity,new String[]{requestPermission},requestCode);
            }
        });
    }

    /* 将弹出对话框的过程封装*/
    public static void showMessageOKCancel(Activity activity,String msg,DialogInterface.OnClickListener oklistener){
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle("提示信息:").setMessage(msg)
                .setPositiveButton("OK", oklistener);
        builder.setNegativeButton("Cancel",null);
        builder.create().show();
    }


}